<?php

function first_word($s) {
	$words = explode(" ", $s);
	return $words[0];
}

class LJ_Import {

	var $file;

	function header() {
		echo '<div class="wrap">';
		echo '<h2>'.__('Import LiveJournal').'</h2>';
	}

	function footer() {
		echo '</div>';
	}

	function unhtmlentities($string) { // From php.net for < 4.3 compat
		$trans_tbl = get_html_translation_table(HTML_ENTITIES);
		$trans_tbl = array_flip($trans_tbl);
		return strtr($string, $trans_tbl);
	}
	
	function greet() {
		echo '<p>'.__('This importer allows you to extract posts from LiveJournal XML export file into your blog.  Pick a LiveJournal file to upload and click Import.').'</p>';
		echo '<p>'.__('For more information, see <a href="http://wiki.lyceum.ibiblio.org/index.php/LiveJournal_importer">the documentation on the wiki</a>').'</p>';
		wp_import_upload_form("admin.php?import=livejournal&amp;step=1");
	}

	function import_posts() {
		global $wpdb, $current_user;

		// Figure out which users are allowed to post on this blog
		// (We'll need it later when we handle the <author> tag.)
		global $blog;
		$allowed_authors = $wpdb->get_results("
			SELECT DISTINCT * 
			FROM $wpdb->users, $wpdb->usermeta 
			WHERE 
			   blog = '$blog' and 
			   ID = user_id 
			ORDER BY user_login
		");

		set_magic_quotes_runtime(0);
		$importdata = file($this->file); // Read the file into an array
		$importdata = implode('', $importdata); // squish it
		$importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);

		preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
		$posts = $posts[1];
		unset($importdata);
		echo '<ol>';		
		foreach ($posts as $post) {
			flush();
			preg_match('|<subject>(.*?)</subject>|is', $post, $post_title);
			$post_title = addslashes(trim($post_title[1]));

			preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date);
			$post_date = strtotime($post_date[1]);
			$post_date = gmdate('Y-m-d H:i:s', $post_date);

			preg_match('|<event>(.*?)</event>|is', $post, $post_content);
			$post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
			$post_content = $this->unhtmlentities($post_content);

			// Clean up content
			$post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
			$post_content = str_replace('<br>', '<br />', $post_content);
			$post_content = str_replace('<hr>', '<hr />', $post_content);

			// If the post title is empty, use the first word of the post
			if ( empty($post_title) ) {
				$post_title = addslashes(first_word($post_content));
			}

			$post_content = addslashes($post_content);

			// Assume that the post is written by the currently-logged-in user
			$post_author = $current_user->ID;
			$post_status = 'publish';

			// Unless there is an "author" element!
			preg_match('|<author>(.*?)</author>|is', $post, $post_author_name);
			if ( ! empty ($post_author_name) ) {
				$post_author_info = get_userdatabylogin($post_author_name[1]);
				// If this username is in the list of allowed authors
				// then we will set post_author to this guy.
				$potential_post_author = $post_author_info->ID;
				if (in_array($potential_post_author, $allowed_authors)) {
				  $post_author = $potential_post_author;
				}
				// else we continue as normal, setting the post author to the currently logged-in user.
			}

			// By default posts are public
			// Unless LJ had them as security=private or security=usemask (friends only)!  Then make it private.
			preg_match('|<security>(.*?)</security>|is', $post, $post_security_value);
			if ( ! empty ($post_security_value) ) {
				$post_security_value = $post_security_value[1];
				if ( ($post_security_value == 'private') || ($post_security_value == 'usemask')) {
					$post_status = 'private';
				}
			}

			echo '<li>';
			if ($post_id = post_exists($post_title, $post_content, $post_date)) {
				printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
			} else {
				printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
				$postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
				$post_id = wp_insert_post($postdata);
				if (!$post_id) {
					_e("Couldn't get post ID");
					echo '</li>';
					break;
				}
			}

			preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
			$comments = $comments[1];
			
			if ( $comments ) {
				$comment_post_ID = (int) $post_id;
				$num_comments = 0;
				foreach ($comments as $comment) {
					preg_match('|<event>(.*?)</event>|is', $comment, $comment_content);
					$comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
					$comment_content = $this->unhtmlentities($comment_content);

					// Clean up content
					$comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
					$comment_content = str_replace('<br>', '<br />', $comment_content);
					$comment_content = str_replace('<hr>', '<hr />', $comment_content);
					$comment_content = addslashes($comment_content);

					preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date);
					$comment_date = trim($comment_date[1]);
					$comment_date = date('Y-m-d H:i:s', strtotime($comment_date));

					preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
					$comment_author = addslashes(trim($comment_author[1]));

					preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
					$comment_author_email = addslashes(trim($comment_author_email[1]));

					$comment_approved = 1;
					// Check if it's already there
					if (!comment_exists($comment_author, $comment_date)) {
						$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
						$commentdata = wp_filter_comment($commentdata);
						wp_insert_comment($commentdata);
						$num_comments++;
					}
				}
			}
			if ( $num_comments ) {
				echo ' ';
				printf(__('(%s comments)'), $num_comments);
			}
			echo '</li>';
			flush();
			ob_flush();
		}
		echo '</ol>';
	}

	function import() {
		$file = wp_import_handle_upload();
		if ( isset($file['error']) ) {
			echo $file['error'];
			return;
		}

		$this->file = $file['file'];
		$this->import_posts();
		wp_import_cleanup($file['id']);
		
		echo '<h3>';
		printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
		echo '</h3>';
	}

	function dispatch() {
		if (empty ($_GET['step']))
			$step = 0;
		else
			$step = (int) $_GET['step'];

		$this->header();
		
		switch ($step) {
			case 0 :
				$this->greet();
				break;
			case 1 :
				check_admin_referer('import-upload');
				$this->import();
				break;
		}
		
		$this->footer();
	}

	function LJ_Import() {
		// Nothing.	
	}
}

$livejournal_import = new LJ_Import();

register_importer('livejournal', __('LiveJournal'), __('Import posts from LiveJournal'), array ($livejournal_import, 'dispatch'));
?>
